/***
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zhaobab.springcloud.web.mall.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.ArrayList;
import java.util.List;

import com.zhaobab.springcloud.web.mall.dto.ProductDto;

/**
 * Product Service
 *
 * @author CD826(CD826Dong@gmail.com)
 * @since 1.0.0
 */
@Service
public class ProductService {
    private List<ProductDto> productList;
    @Autowired
    public ProductService() {
        this.productList = this.buildProducts();
    }
    public List<ProductDto> findAll(){
        return this.buildProducts();
    }

    public ProductDto loadByItemCode(@PathVariable("itemCode") String itemCode){
        for (ProductDto productDto : this.productList) {
            if (productDto.getItemCode().equalsIgnoreCase(itemCode)) { return productDto; }
        }

        return null;
    }

    protected List<ProductDto> buildProducts() {
        List<ProductDto> products = new ArrayList<>();
        products.add(new ProductDto("item-1", "测试商品-1", "TwoStepsFromJava", 100));
        products.add(new ProductDto("item-2", "测试商品-2", "TwoStepsFromJava", 200));
        products.add(new ProductDto("item-3", "测试商品-3", "TwoStepsFromJava", 300));
        products.add(new ProductDto("item-4", "测试商品-4", "TwoStepsFromJava", 400));
        products.add(new ProductDto("item-5", "测试商品-5", "TwoStepsFromJava", 500));
        products.add(new ProductDto("item-6", "测试商品-6", "TwoStepsFromJava", 600));
        return products;
    }
}
